package org.cigo.timemachine;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class CompassDemo extends MapActivity {
	
	private SensorManager mSensorManager;
	private MapView mMapView;
	private MyLocationOverlay mMyLocationOverlay;
    private RotateView mRotateView;
    
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private class RotateView extends ViewGroup implements SensorEventListener {

		private static final float SQ2 = 1.414213562373095f;
        private final SmoothCanvas mCanvas = new SmoothCanvas();
        private float mHeading = 0;
        
		public RotateView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onLayout(boolean changed, int l, int t, int r, int b) {
			// TODO Auto-generated method stub
			final int width = getWidth();
            final int height = getHeight();
            final int count = getChildCount();
            for (int i = 0; i < count; i++) {
                final View view = getChildAt(i);
                final int childWidth = view.getMeasuredWidth();
                final int childHeight = view.getMeasuredHeight();
                final int childLeft = (width - childWidth) / 2;
                final int childTop = (height - childHeight) / 2;
                view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
            }
		}

		@Override
		protected void dispatchDraw(Canvas canvas)
		{
			canvas.save(Canvas.MATRIX_SAVE_FLAG);
            canvas.rotate(-mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
            mCanvas.delegate = canvas;
            super.dispatchDraw(mCanvas);
            canvas.restore();
		}
		
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}
		
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
            int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
            int sizeSpec;
            if (w > h) {
                sizeSpec = MeasureSpec.makeMeasureSpec((int) (w * SQ2), MeasureSpec.EXACTLY);
            } else {
                sizeSpec = MeasureSpec.makeMeasureSpec((int) (h * SQ2), MeasureSpec.EXACTLY);
            }
            final int count = getChildCount();
            for (int i = 0; i < count; i++) {
                getChildAt(i).measure(sizeSpec, sizeSpec);
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub
			synchronized (this) {
                mHeading = event.values[0];
                invalidate();
            }
		}
		
	}
	
    @Override
    protected void onResume() {
        super.onResume();
        List<Sensor> lists = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
        if(!lists.isEmpty())
        {
        	Sensor sensor = lists.get(0);
        	mSensorManager.registerListener(mRotateView,
        		 sensor,SensorManager.SENSOR_DELAY_UI);
        }
        mMyLocationOverlay.enableMyLocation();
    }
    
    @Override
    protected void onStop() {
        mSensorManager.unregisterListener(mRotateView);
        mMyLocationOverlay.disableMyLocation();
        super.onStop();
    }
    

    @Override
    public void onCreate(Bundle savedInstanceState){
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mRotateView = new RotateView(this);
        mMapView = new MapView(this, getString(R.string.map_api_key));
        setContentView(mRotateView);
        
        mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
        mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
            mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
        }});
        mMapView.getOverlays().add(mMyLocationOverlay);
        mMapView.getController().setZoom(18);
        mMapView.setClickable(true);
        mMapView.setEnabled(true);
    }
}
